import { ProTable, ActionType } from '@ant-design/pro-components';
import React, { useRef } from 'react';
import { message, Space, Button, Modal } from 'antd';
import { useRequest } from 'ahooks';
import { roleV1AdminControllerDestroy, roleV1AdminControllerAll } from '@/services/auto/roleAdmin';
import Add from './component/Add';

export default () => {
  const actionRef = useRef<ActionType>();

  const { runAsync: deleteRun } = useRequest(roleV1AdminControllerDestroy, {
    manual: true,
    onSuccess: () => {
      message.success('删除成功');
      actionRef.current?.reload();
    },
  });

  return (
    <>
      <ProTable<API.RoleAdminAllResult>
        actionRef={actionRef}
        rowKey="id"
        toolBarRender={() => [<Add key="add" reload={()=>{
          actionRef.current?.reload();
        }}>
          <Button type="primary">
            添加角色
          </Button>
        </Add>]}
        pagination={false}
        search={false}
        request={() => {
          return roleV1AdminControllerAll().then((res) => {
            return {
              success: true,
              data: res,
            };
          });
        }}
        columns={[
          {
            title: '角色名称',
            dataIndex: 'name',
          },
          {
            title: '角色描述',
            dataIndex: 'desc',
            search: false,
          },
          {
            title: '添加时间',
            valueType: 'dateTime',
            dataIndex: 'createTime',
            search: false,
          },
          {
            align: 'center',
            title: '操作',
            key: 'option',
            valueType: 'option',
            width: 120,
            render: (_, record) => {
              return (
                <Space direction="vertical" size={0}>
                  <Add
                    reload={actionRef.current?.reload}
                    data={{
                      ...record,
                    }}
                  >
                    <Button type="link">
                      编辑
                    </Button>
                  </Add>

                  {record.id !== 1 && <Button type="link" danger onClick={()=>{
                    Modal.confirm({
                      title: '删除后不可恢复，确定删除当前角色？',
                      onOk: ()=>{
                        return deleteRun({uuid: record.uuid});
                      }
                    })
                  }}>
                    删除
                  </Button>}
                </Space>
              );
            },
          },
        ]}
      />
    </>
  );
};
